package com.gali.juc;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * @Auther: gali
 * @Date: 2022-08-18 20:47
 * @Description:
 */
public class CompletableFutureApi {
    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
        CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "abc";
        });
//        System.out.println(stringCompletableFuture.get());
//        String s = stringCompletableFuture.get(1, TimeUnit.SECONDS);
//        System.out.println(stringCompletableFuture.join());
//        System.out.println(stringCompletableFuture.getNow("xxx"));
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(stringCompletableFuture.complete("complete")+stringCompletableFuture.join());

    }
}
